<?php
defined('_VISIT_URL_') or exit('No direct script access allowed');

class housecraft {

	//保存家政服务信息
	public function saveHousecraft() {
		if (getPost('flag') != 'add' && getPost('flag') != 'edit') {
			return util::getValidError('保存操作标识异常');
		}

		if (empty(getPost('title')) || valid::isWicked(getPost('title'))) {
			return util::getValidError('标题格式异常');
		}

		if (getPost('sortation') == '') {
			return util::getValidError('请选择栏目分类');
		}

		if (!empty(getPost('address')) && valid::isWicked(getPost('address'))) {
			return util::getValidError('商家地址格式异常');
		}

		if (empty(getPost('person')) || valid::isWicked(getPost('person'))) {
			return util::getValidError('联系人格式异常');
		}

		if (empty(getPost('phone')) || (!valid::isMobile(getPost('phone')) && !valid::isTelphone(getPost('phone')))) {
			return util::getValidError('联系电话格式异常');
		}

		if (!empty(getPost('remark')) && valid::isWicked(getPost('remark'))) {
			return util::getValidError('备注格式异常');
		}

		if (getPost('sortation') == '0' && getPost('identity') == '') {
			return util::getValidError('月嫂保姆类必须选择服务者身份');
		}

		if (empty(getPost('introduce'))) {
			return util::getValidError('请填写服务详情');
		}

		$rst = new sqlcon();
		$uuid = '';

		if (getPost('flag') == 'add') {
			if (empty(getPost('expiretime'))) {
				return util::getValidError('请填写到期时间');
			}

			$uuid = util::getUUID();

			//写入服务详情
			$introducePath = 'upload/housecraft/introduce/' . $uuid . '.txt';
			$introduceFile = $uuid . '.txt';
			util::writeFile('upload/housecraft/introduce', $introduceFile, getPost('introduce'));

			//格式化服务特征的分隔逗号
	        $feature = getPost('feature');
	        if (!empty($feature)) {
	        	$feature = str_replace('，', ',', $feature);
				$lastStr = substr($feature, -1);
				if ($lastStr == ',' || $lastStr == '，') {
					$feature = substr($feature, 0, -1);
				}
				$feature = str_replace(',', '，', $feature);
	        }

	        //保存详情大图
	        $bigPicList = json_decode($_POST['bigpic'], true)[0];
	        $tempPicPath = array();
	        foreach ($bigPicList as $value) {
	        	$tempPath = '';
	        	if (strpos($value, ';base64,')) {
	        		$tempPath = 'upload/housecraft/samplepic/' . util::getFileName($value);
		            util::saveFile($value, _IMGS_PATH_ . $tempPath);
		            $tempPicPath[] = $tempPath;
	        	}
	        }

	        //保存图片记录
	        for ($i = 0; $i < count($tempPicPath); $i++) {
	        	$rst->addCollect('housecraftid', $uuid);
	        	$rst->addCollect('path', $tempPicPath[$i]);
	        	$rst->addCollect('sx', $i + 1);
	        	$rst->insertInfo('housecraftpic');
	        }

	        //保存压缩的列表小图
	        $smallPath = '';
	        if (!empty(getPost('coverid'))) {
	        	foreach ($bigPicList as $key => $value) {
	        		if ($key == getPost('coverid')) {
	        			if (strpos($value, ';base64,')) {
				            $smallPath = 'upload/housecraft/samplepic/' . util::getFileName($value);
				            $fileSize = util::getFileSize($value);

				            require('php/library/compress.php');
				            if ($fileSize > 500) {
				            	(new compress($value, 1))->compressImg(_IMGS_PATH_ . $smallPath);
				            }
				            else {
			                    util::saveFile($value, _IMGS_PATH_ . $smallPath);
			                }
				        }
	        		}
	        	}
	        }

	        //保存信息
			$rst->addCollect('housecraftid', $uuid);
			$rst->addCollect('title', getPost('title'));
			$rst->addCollect('company_verifyid', '');
			$rst->addCollect('sortation', getPost('sortation'));
			$rst->addCollect('businessid', '');
			$rst->addCollect('business', '');
			$rst->addCollect('address', getPost('address'));
			$rst->addCollect('position', '');
			$rst->addCollect('person', getPost('person'));
			$rst->addCollect('phone', getPost('phone'));
			$rst->addCollect('identity', getPost('identity'));
			$rst->addCollect('feature', $feature);
			$rst->addCollect('area', getPost('area'));
			$rst->addCollect('introduce', $introducePath);
			$rst->addCollect('smallpic', $smallPath);
			$rst->addCollect('browse', '0');
			$rst->addCollect('click', '0');
			$rst->addCollect('dial', '0');
			$rst->addCollect('istop', '0');
			$rst->addCollect('toptype', '0');
			$rst->addCollect('refreshcount', '0');
			$rst->addCollect('isautorefresh', '0');
			$rst->addCollect('status', getPost('status'));
			$rst->addCollect('sort', '0');
			$rst->addCollect('clicktime', '');
			$rst->addCollect('dialtime', '');
			$rst->addCollect('refreshtime', date('Y-m-d H:i:s'));
			$rst->addCollect('expiretime', getPost('expiretime') . ' ' . date('H:i:s'));
			$rst->addCollect('topstarttime', '');
			$rst->addCollect('topendtime', '');
			$rst->addCollect('remark', getPost('remark'));
			$rst->insertInfo('housecraft');
		}
		else {

		}

		$result = util::getSuccess('success');
	    $result['housecraftid'] = $uuid;
		return $result;
	}

}